package com.example.tianling.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.tianling.common.vo.productVo;
import com.example.tianling.dao.CommentMapper;
import com.example.tianling.dao.ProductMapper;

import com.example.tianling.entity.ProductEntity;

import com.example.tianling.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class ShopServiceImpl
        extends ServiceImpl<ProductMapper, ProductEntity>
        implements ShopService {
    @Autowired
    ProductMapper productMapper;
    @Override
    public Map<String, List<productVo>> getProducts(){
        QueryWrapper<ProductEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.orderByAsc("now_price");
        queryWrapper.select("pro_id,cate_id,name,subtitle,main_image,detail,now_price,stock,origin_price,is_vip");
        List<productVo> productList = productMapper.selectList(queryWrapper).stream()
                .map(this::mapToProductVo)
                .collect(Collectors.toList());
        Map<String, List<productVo>> result = new HashMap<>();
        for (productVo product : productList) {
            String cateId = product.getCateId();
            result.computeIfAbsent(cateId, k -> new ArrayList<>()).add(product);
        }
        return result;
    }

    private productVo mapToProductVo(ProductEntity entity) {
        System.out.println(entity);
        productVo vo = new productVo();
        vo.setId(entity.getProId());
        vo.setName(entity.getName());
        vo.setIcon(entity.getMainImage());
        vo.setDescription(entity.getDetail());
        vo.setPrice(entity.getNowPrice());
        vo.setIsVip(entity.getIsVip()==1);
        vo.setCateId(entity.getCateId());
        return vo;
    }
}
